from collections import Counter
from typing import List

import pandas
from imblearn.over_sampling import BorderlineSMOTE
from scipy import stats
from sklearn.metrics import auc, roc_curve, PrecisionRecallDisplay
from sklearn.model_selection import train_test_split


def single_model_runner(
    model,
    predicted: str,
    predictors: List[str],
    start: str = "2007-01-01",
    end="2025-12-31",
):
    dataset = pandas.read_csv("./data/data.csv", sep="\t")
    dataset = dataset[dataset["accper"] > f"{start}"]
    dataset = dataset[dataset["accper"] <= f"{end}"]
    dataset = dataset.reset_index(drop=True)

    X = stats.zscore(dataset[predictors])
    y = dataset[predicted]
    X_train, X_test, y_train, y_test = train_test_split(
        X, y, test_size=0.3, random_state=1
    )
    oversample = BorderlineSMOTE()
    X_train, y_train = oversample.fit_resample(X_train, y_train)

    model.fit(X_train, y_train)
    predict_result = model.predict_proba(X_test)
    fpr, tpr, _ = roc_curve(y_test, predict_result[:, 1], pos_label=1)
    auc_value = auc(fpr, tpr)
    ap = PrecisionRecallDisplay.from_estimator(model, X_test, y_test).average_precision
    return {"auc": auc_value, "ap": ap}
